package com.webdesign.service.impl;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webdesign.dao.jdbc.TbAnnounceSQL;
import com.webdesign.dao.mapper.TbAnnounceMapper;
import com.webdesign.dto.DisplayDTO;
import com.webdesign.entity.TbAnnounce;
import com.webdesign.entity.TbAnnounceExample;
import com.webdesign.entity.TbDisply;
import com.webdesign.entity.TbDisplyExample;
import com.webdesign.entity.TbUser;
import com.webdesign.entity.TbUserhomework;
import com.webdesign.service.ITbAnnounceService;

@Service("TbAnnounceService")
public class TbAnnounceService implements ITbAnnounceService {
	
	@Autowired
	private TbAnnounceMapper tbAnnounceMapper;
	
	/*发布公告*/
	@Override
	public Boolean submitAnnounce(String title, String content, Byte state) {
		TbAnnounce tbAnnounce=new TbAnnounce();
		TbAnnounceSQL tbAnnounceSQL = new TbAnnounceSQL();
		Integer maxid = tbAnnounceSQL.getMaxAnnounceId();
		tbAnnounce.setId(maxid+1);
		tbAnnounce.setTitle(title);
		tbAnnounce.setState(state);
		tbAnnounce.setContent(content);
		Date now = new Date();
		tbAnnounce.setCreatetime(now);
		if (tbAnnounceMapper.insertSelective(tbAnnounce) == 1) {
			return true;
		} else {
			return false;
		}
	}


	@Override
	public List<TbAnnounce> announcedisplay() {
		List<TbAnnounce> displayAnnounList = new ArrayList<TbAnnounce>();
		TbAnnounceExample tbAnnounceExample = new TbAnnounceExample();
		tbAnnounceExample.createCriteria().getCriteria();
		List<TbAnnounce> tbAnnounceList = tbAnnounceMapper.selectByExample(tbAnnounceExample);
		for(TbAnnounce tbAnnounce: tbAnnounceList){
			String title = tbAnnounce.getTitle();
			Byte flag=tbAnnounce.getState();
			if(flag==0){
				TbAnnounce teacherannoun=new TbAnnounce();
				teacherannoun.setTitle(title);
				displayAnnounList.add(teacherannoun);
			}
			else if(flag==1){
					TbAnnounce studentannoun=new TbAnnounce();
					studentannoun.setTitle(title);
					displayAnnounList.add(studentannoun);				
			}
		}
		Collections.sort(displayAnnounList,new Comparator(){
            public int compare(TbAnnounce arg0, TbAnnounce arg1) {
                return arg1.getCreatetime().compareTo(arg0.getCreatetime());
            }

			@Override
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				return 0;
			}
        });
		// TODO Auto-generated method stub
		return displayAnnounList;
	}


	@Override
	public TbAnnounce getAnnouninfor(int announid) {
		TbAnnounce tbAnnounce=tbAnnounceMapper.selectByPrimaryKey(announid);
		TbAnnounce announInfor=new TbAnnounce();
		announInfor.setTitle(tbAnnounce.getTitle());
		announInfor.setContent(tbAnnounce.getContent());
		announInfor.setCreatetime(tbAnnounce.getCreatetime());
		
		// TODO Auto-generated method stub
		return announInfor;
	}

	

}
